//
//  HTTPTOOL.m
//  GlobalBuy
//
//  Created by dllo on 15/9/22.
//  Copyright (c) 2015年 Jessica Tong. All rights reserved.
//

#import "AFTool.h"

@implementation AFTool

+ (void)GETWithURL:(NSString *)url body:(NSDictionary *)body httpHead:(NSDictionary *)head responseStyle:(responseStyle)style success:(void(^)(id result))success fail:(void(^)(NSError *error))fail
{
    /* 1. 创建一个网络请求管理者 */
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    /* 2. 判断是否有请求头, 有就添加在请求中 */
    if (head) {
        for (NSString *key in head) {
            [manager.requestSerializer setValue:[head objectForKey:key] forHTTPHeaderField:key];
        }
    }
    
    /* 3. 判断传入数据类型 */
    switch (style) {
        case DATA: {
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        }
        case JSON: {
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        }
        case XML: {
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        }
        default:
            break;
    }
    
    /* 4. 设置请求接受的数据类型, 在初始化之后 */
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil]];
    
    /* 5. get请求 */
    [manager GET:url parameters:body success:^void(AFHTTPRequestOperation *peration , id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^void(AFHTTPRequestOperation *request , NSError *error) {
        if (fail) {
            fail(error);
        }
    }];
}


+ (void)POSTWithURL:(NSString *)url body:(id)body bodyStyle:(bodyStyle)bodyStyle httpHead:(NSDictionary *)head responseStyle:(responseStyle)style success:(void(^)(id result))success fail:(void(^)(NSError *error))fail
{
    /* 1. 创建一个网络请求管理者 */
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    
    /* 2. 处理body类型 */
    switch (bodyStyle) {
        case JSONStyle: {
            manager.requestSerializer = [AFJSONRequestSerializer serializer];
            break;
        }
        case stringStyle: {
            [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString *(NSURLRequest *request, id parameters, NSError **error) {
                return parameters;
            }];
            break;
        }
        default:
            break;
    }
    
    /* 3. 判断是否有请求头, 有就添加在请求中 */
    if (head) {
        for (NSString *key in head) {
            [manager.requestSerializer setValue:[head objectForKey:key] forHTTPHeaderField:key];
        }
    }
    
    /* 5. 判断传入数据类型 */
    switch (style) {
        case DATA: {
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        }
        case JSON: {
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        }
        case XML: {
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        }
        default:
            break;
    }
    
    /* 4. 设置接收返回数据的类型 , 在初始化之后*/
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil]];
    
    /* 6. POST请求 */
    [manager POST:url parameters:body success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (fail) {
            fail(error);
        }
    }];
}



@end
